package com.tv189.springbatchtest.paramvalue;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName: JobConfiguration
 * @Description: TODO
 * @Author: 19202
 * @Date: 2025/4/20 22:05
 * @Version 1.0
 **/
@SpringBootApplication
@EnableBatchProcessing
@Configuration
public class ParaValueJobConfiguration {
    /**
     * JobBuilderFactory
     */
    private JobBuilderFactory jobBuilderFactory;

    private StepBuilderFactory stepBuilderFactory;

    public ParaValueJobConfiguration(JobBuilderFactory jobBuilderFactory,
                                     StepBuilderFactory stepBuilderFactory) {
        this.jobBuilderFactory = jobBuilderFactory;
        this.stepBuilderFactory = stepBuilderFactory;
    }

    /**
     * 如果一个作业的名字是一样的，
     * 执行作业的参数也是一样的，如果这个作业已经被成功的执行过一次了，那么再次被执行的时候就被重复执行了
     *
     * @return {@link Job }
     */
    @Bean
    public Job paramJob() {
        return jobBuilderFactory.get("param-job")
                .start(paramStep1())
                .build();
    }

    /**
     * 微
     *
     * @return {@link Tasklet }
     */
    @StepScope
    @Bean
    public Tasklet paramTasklet(@Value("#{jobParameters['name']}") String name) {
        return (stepContribution, chunkContext) -> {
            System.out.println("param SpringBatch ......" + name);
            return RepeatStatus.FINISHED;
        };
    }

    /**
     * step1
     *
     * @return {@link Step }
     */
    @Bean
    public Step paramStep1() {
        return stepBuilderFactory.get("step1").tasklet(paramTasklet(null)).build();
    }

    public static void main(String[] args) {
        SpringApplication.run(ParaValueJobConfiguration.class, args);
    }
}
